package ee.ttu.lecturerate.http;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.util.Log;

public class AsyncTaskExecutor
{
	private List<RequestAsyncTask> taskList;
	private List<RequestInput> inputList;
	private List<ResponseResult> resultList;
	private Activity currentActivity;
	
	public AsyncTaskExecutor(List<RequestInput> inputs, Activity activity)
	{
		currentActivity = activity;
		inputList = inputs;
		taskList = new ArrayList<RequestAsyncTask>();
		resultList = new ArrayList<ResponseResult>();
		for(int n=0; n!=inputs.size(); n++) {
			taskList.add(new RequestAsyncTask(this, activity));
		}
	}
	
	public void execute()
	{
		for(int n=0; n!=inputList.size(); n++) {
			taskList.get(n).execute(inputList.get(n));
		}
	}
	
	public void onPostExecute()
	{
		Log.d("AsyncTaskExecutor.onPostExecute()", "All tasks completed");
		ResponseManager manager = new ResponseManager(currentActivity);
		manager.manage(resultList);
	}
	
	public void addResult(ResponseResult result)
	{
		resultList.add(result);
	}
	
	public boolean checkAllDone()
	{
		if(inputList.size() == resultList.size()) return true;
		else {
			Log.d("AsyncTaskExecutor.checkAllDone()", 
					String.valueOf(inputList.size() - resultList.size()) + " still running");
			return false;
		}
	}
}
